package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        //既要保存套餐的基本信息 又要保存套餐和菜品的关联信息
        setmealService.saveWithDish(setmealDto);
        return R.success("新建套餐保存成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //1.创建分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        //2.创建查询条件构造器
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //3.添加查询条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        lambdaQueryWrapper.orderByAsc(Setmeal::getUpdateTime);
        //4.执行分页查询
        setmealService.page(pageInfo, lambdaQueryWrapper);
        //5.对象拷贝
        BeanUtils.copyProperties(pageInfo, dtoPage);
        //6.获取本次分页查询到的数据集合
        List<Setmeal> records = pageInfo.getRecords();
        //创建dtoList 用来保存处理好的dto数据 作为分页展示数据返回给前端
        List<SetmealDto> dtoList = records.stream().map((setmeal) -> {
            Long categoryId = setmeal.getCategoryId(); //获取当前套餐的分类id
            Category category = categoryService.getById(categoryId);    //获取到分类的对象
            String categoryName = category.getName();   //获取当前分类的名称
            SetmealDto setmealDto = new SetmealDto();    //创建dto对象
            BeanUtils.copyProperties(setmeal, setmealDto);    //把setmeal中的数据拷贝到dto中
            setmealDto.setCategoryName(categoryName);  //把分类的名称赋值给dto
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(dtoList);    //设置处理好的dtoList给分页插件
        //7.返回查询结果
        return R.success(dtoPage);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        //根据id查询套餐信息并且包含菜品信息
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        //修改菜品的基本信息和口味信息
        setmealService.updateWithDish(setmealDto);
        return R.success("修改套餐信息成功");
    }

    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status, Long[] ids){
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        for (Long id : ids) {
            setmeal.setId(id);
            setmealService.updateById(setmeal);
        }
        return R.success("套餐状态修改成功");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        //根据id删除 判断删除的套餐是否在启售状态 如果启售 不能删除
        //删除套餐的同时 也需要删除套餐的菜品关联表
        setmealService.removeWithDish(ids);
        return R.success("删除套餐成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<SetmealDto>> list(Setmeal setmeal){
        //1.创建查询条件构造器
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //2.添加查询条件
        lambdaQueryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId()).eq(Setmeal::getStatus, setmeal.getStatus());
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //3.执行查询
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);
        //根据菜品的id获取对应口味信息封装到dto对象中
        List<SetmealDto> setmealDtoList = list.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            //获取分类的id
            Long categoryId = item.getCategoryId();
            //根据分类的id查询出分类的对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            //获取当前菜品的id
            Long setmealId = item.getId();
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealId);
            List<SetmealDish> dishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
            setmealDto.setSetmealDishes(dishList);
            return setmealDto;
        }).collect(Collectors.toList());
        //4.返回执行结果
        return R.success(setmealDtoList);
    }

//    @GetMapping("/dish/{id}")
//    public R<List<SetmealDish>> getDishBySetmealId(@PathVariable Long id){
//        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
//        List<SetmealDish> list = setmealDishService.list(lambdaQueryWrapper);
//        return R.success(list);
//    }
}
